<?php

namespace app\api\controller;

use think\Db;
use app\api\model\Topup;
use app\api\model\OrderCommon;
use app\api\model\ConfigSystem as Config;

class Wap extends Common
{
    public function pay($order_sn)
    {
        $out_trade_no = $order_sn;

        $state = 0;
        $memberid = 0;
        # 充值
        $data = Db::name('topup')->master()->where(['numbers' => $out_trade_no, 'state' => 0])->find();

        if ($data) {
            $state = 1;
            $money = $data['price'];
            $memberid = $data['fk_userid'];
        }

        # 付费订单
        if (!$state) {
            // $data = OrderCommon::get(['number' => $out_trade_no, 'status' => 0]);
            $data = Db::name('order_common')->master()->where(['number' => $out_trade_no, 'status' => 0])->find();

            if ($data) {
                $state = 1;
                $money = $data['money'];
            }
        }
        # 付费订单
        if (!$state) {
            $data = Db::name('mall_order')->master()->where(['number' => $out_trade_no, 'status' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['money'];
            }
        }

        # 服务订单
        if (!$state) {
            $data = Db::name('order_of_service')->master()->where(['number' => $out_trade_no, 'paystate' => 0, 'state' =>0 ])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }
        
        # Flag订单
        if (!$state) {
            $data = Db::name('user_flag')->master()->where(['number' => $out_trade_no, 'paystate' => 0, 'status' =>0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }

        if (!$state) {
            $data = Db::name('user_address_red_packet')->master()->where(['number' => $out_trade_no, 'type' => 1, 'paystate' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['totalprice'];
            }
        }

        if (!$state) {
            $data = Db::name('order_of_addressgood')->master()->where(['number' => $out_trade_no, 'status' => 0, 'paystate' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }
        if (!$state) {
            $data = Db::name('activity_join')->master()->where(['number' => $out_trade_no, 'paystate' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }
        

        if (!$state) {
            exit('订单不存在');
        } else {
            $this->assign('order_sn', $order_sn);
            $this->assign('total_amount', $money);
        }

        $config = Config::index('pay');
        $this->assign('config', $config);

        $this->assign('url', ['wechat' => 'api/Wechatwap/app', 'alipay' => 'api/Alipaywap/tradeAppPay']);

        $this->assign('name', getenv('APP_NAME'));

        $this->assign('memberid', $memberid);
        $this->assign('wxauto', input('wxauto/d', 0));

        return $this->fetch();
    }
}
